package com.jy.reggie.common;


import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 异常处理方法
     * @param ex
     * @return
     */
    @ExceptionHandler(Exception.class)//捕获所有异常
    public R<String> ex(Exception ex){
        log.info(ex.getMessage());

        if(ex.getMessage().contains("Duplicate entry")){
            String[] splits = ex.getMessage().split(" ");
            String msg = "已存在";
            return R.error(msg);
        }
        return R.error("对不起，操作失败，请联系管理员");
    }


    /**
     * 业务异常处理方法
     * @param ex
     * @return
     */
    @ExceptionHandler(CustomException.class)//捕获业务异常类
    public R<String> ex(CustomException ex){
        log.info(ex.getMessage());
        return R.error(ex.getMessage());
    }
}
